﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DTO;
using DAO;

namespace BUS
{
    public class LoaiSanPham_BUS
    {
        private static LoaiSanPham_BUS m_instance = null;

        public static LoaiSanPham_BUS Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new LoaiSanPham_BUS();
                return m_instance;
            }
        }

        /// <summary>
        /// Lấy 1 loại sp theo mã loại sản phẩm
        /// </summary>
        /// <param name="maLoaiSP"></param>
        /// <returns></returns>
        public LoaiSanPham_DTO LayLoaiSP(string maLoaiSP)
        {
            DataTable dt = LoaiSanPham_DAO.Instance.LayLoaiSP(maLoaiSP);
            // Bi loi
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return null;

            DataRow row = list[0];
            string maLoai = (string)row["MaLoaiSanPham"];
            string tenLoai = (string)row["TenLoaiSanPham"];

            return new LoaiSanPham_DTO(maLoai, tenLoai);
        }

        /// <summary>
        /// Lấy toàn bộ loại sãn phẩm
        /// </summary>
        /// <returns></returns>
        public List<LoaiSanPham_DTO> LayToanBoLoaiSP()
        {
            List<LoaiSanPham_DTO> result = new List<LoaiSanPham_DTO>();

            DataTable dt = LoaiSanPham_DAO.Instance.LayToanBoLoaiSanPham();
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return null;
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = list[i];
                string maLoai = (string)row["MaLoaiSanPham"];
                string tenLoai = (string)row["TenLoaiSanPham"];

                result.Add(new LoaiSanPham_DTO(maLoai, tenLoai));
            }
            return result;
        }

        /// <summary>
        ///   them loai san pham moi
        /// </summary>
        /// 
        public bool ThemLoaiSanPham(LoaiSanPham_DTO item)
        {
            return LoaiSanPham_DAO.Instance.ThemLoaiSanPham(item.TenLoaiSanPham);
        }
        /// <summary>
        ///   sua loai san pham
        /// </summary>
        /// 
        public bool SuaLoaiSanPham(LoaiSanPham_DTO item)
        {
            return LoaiSanPham_DAO.Instance.SuaLoaiSanPham(item.MaLoaiSanPham, item.TenLoaiSanPham);
        }
        /// <summary>
        ///   xoa loai san pham
        /// </summary>
        /// 
        public bool XoaLoaiSanPham(LoaiSanPham_DTO item)
        {
            return LoaiSanPham_DAO.Instance.XoaLoaiSanPham(item.MaLoaiSanPham);
        }
    }
}
